#!/bin/bash

# Edit this to reflect where the XNT PreArchive directory should be.
XNAT_PREARCHIVE=/mnt/hgfs/Share/PreArchive;

# Get prerequesites
sudo apt-get -y install libimage-exiftool-perl libimage-info-perl dcmtk subversion emacs22-nox perl-doc libclass-dbi-mysql-perl dcmtk-doc dcmtk-www libdcmtk1-dev


# Check for required libs
for lib in XML::Simple; do perl -e "use $lib;" && echo "Found $lib ... OK" || echo "ERROR: $lib not found"; done

# Remove dicomserver from startup
if [ -e /etc/init.d/dicomserver ]; then
    sudo /etc/init.d/dicomserver stop && \
	sudo mkdir -v /etc/init.d/disabled && \
	sudo mv -v /etc/init.d/dicom* /etc/rc2.d/S99dicomserver /etc/init.d/disabled || \
	exit -1;
fi

# Install script
if [ ! -e /XNAT/Tools ]; then
    sudo mkdir -p /XNAT/Tools && \
	sudo chown -Rc vmuser:vmuser /XNAT/Tools || \
	exit -1;
fi

# Change into the XNAT Tools directory
cd /XNAT/Tools || exit -1;
# Checkout the code (including this file, of course)
svn checkout http://perl-xnat.googlecode.com/svn/trunk/ perl-xnat || exit -1;

# Make sure everything is OK with dcm2xnat
perl -c -w /XNAT/Tools/perl-xnat/dcm2xnat || exit -1;
perl -w /XNAT/Tools/perl-xnat/dcm2xnat --help || exit -1;

# Create the PreArchive directory if it is not there already
if [ ! -e $XNAT_PREARCHIVE ]; then
    mkdir XNAT_PREARCHIVE || exit -1;
fi

# Test storescp
mkdir -pv /mnt/hgfs/Share/PreArchive && \
storescp --verbose --filename-extension ".dcm" --output-directory "/mnt/hgfs/Share/PreArchive" --exec-on-eostudy '/XNAT/Tools/perl-xnat/dcm2xnat -v --extra --storescp #p' --aetitle "GSP" --sort-conc-studies "GSPTEST" --eostudy-timeout 10 4444


